package com.lumis.utils;

import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import org.springframework.stereotype.Component;

@Component
public class FotosUtils {
	
	 public BufferedImage resizeImage(BufferedImage bimg, int maxPixels){
		    
	    	int type = bimg.getType() == 0? BufferedImage.TYPE_INT_ARGB : bimg.getType();
	    	int newWidth = bimg.getWidth();
	    	int newHeight = bimg.getHeight();
	    	
	    	
	    	BufferedImage resizedImage = new BufferedImage(bimg.getWidth(), bimg.getHeight(), type);
	    	
	    	while(resizedImage.getWidth() > maxPixels || resizedImage.getHeight() > maxPixels){
	    		newWidth = (int) (resizedImage.getWidth() * 0.95);
	    		newHeight = (int) (resizedImage.getWidth() * 0.95);
	    		resizedImage = new BufferedImage( newWidth, newHeight, type);
	    	}
	    	
			Graphics2D g = resizedImage.createGraphics();
			g.drawImage(bimg, 0, 0, newWidth, newHeight, null);
			g.dispose();

			return resizedImage;
	    }


}
